<div class="form-container">
	<form id="form">
		<table class="tableform">
			<tr>
				<th style="width: 150px;">所属菜单：</th>
				<td>
					<input id="cc" name="menu_id" name="text" class="easyui-validatebox" type="text" style="width: 219px; height:25px;"/>
				</td>
			</tr>
			<tr id="treeBox" style="display:none">
				<th>节点：</th>
				<td>
					<input id="resource" name="pid" class="easyui-validatebox" type="text" style="width: 269px; height:25px;" />
				 	<a onClick="$resource.combotree('setValue', '');" class="easyui-linkbutton ml-3" data-options="plain: true"><i class='fa fa-remove fa-120 pt-5 pb-4 fa-c-red'></i></a>
				 	<a onClick="$resource.combotree('reload')" class="easyui-linkbutton ml-3" data-options="plain: true"><i class='fa fa-refresh fa-120 pt-5 pb-4 fa-c-green'></i></a>
				</td>
			</tr>
			<tr>
				<th>名称：</th>
				<td style="position: relative;">
					<input name="text" class="easyui-validatebox" type="text" data-options="required: true" style="float:left;"/>
				</td>
			</tr>
			<tr>
				<th>地址：</th>
				<td>
					<input name="href" class="easyui-validatebox" type="text" />
				</td>
			</tr>
			<tr>
				<th>排序：</th>
				<td>
					<input name="sorting" class="easyui-numberbox" type="text" data-options="prompt: '不填写则为默认排序'" style="width:217px;" />
				</td>
			</tr>
			<tr>
				<th>图标：</th>
				<td>
					<input id="showIcon" name="icon" style="cursor: pointer;background: #ddd" readonly="readonly" class="easyui-validatebox" type="text" value="fa fa-clone" />
				</td>
			</tr>
			<tr>
				<th>图标EasyUI：</th>
				<td>
					<input id="showEasyuiIcon" name="iconCls" style="cursor: pointer;background: #ddd" readonly="readonly" class="easyui-validatebox" type="text" value="icon-standard-application-view-list" />
				</td>
			</tr>
			<tr>
				<th>是否展开：</th>
				<td>
					<div id="icheck">
						<input id="state1" type="radio" name="state" value="open" checked="checked">
						<label class="irl" for="state1">展开</label>　
						<input id="state2" type="radio" name="state" value="close">
						<label class="irl" for="state2">折叠</label>　
					</div>
				</td>
			</tr>
			<tr>
				<th>是否禁用：</th>
				<td>
					<div id="icheck">
						<input id="disabled1" type="radio" name="status" value="false" checked="checked">
						<label class="irl" for="disabled1">正常</label>　
						<input id="disabled2" type="radio" name="status" value="true">
						<label class="irl" for="disabled2">禁用</label>　
					</div>
				</td>
			</tr>
			<tr style="display:none">
				<td>
					<input type="hidden" name="id" value="${id!}">
					<input type="hidden" id="checkId">
				</td>
			</tr>
		</table>
		<br/>
		<table class="tableform">
			<tr>
				<th style="width: 150px;">权限标示：</th>
				<td>
					<input name="identity" class="easyui-validatebox" type="text" data-options="required: true" />
				</td>
			</tr>
			<tr>
				<th>操作权限：</th>
				<td>
					<select id="combogrid-permit" style="width:350px; height: 26px;"></select>
				</td>
			</tr>
		</table>
	</form>
</div>

<script>
	var form_url = "${ctx}/manager/system/resource/add", id = "${id!}", $cc, $resource, $permits ;
	$(function(){
		$('#icheck input').iCheck({
			checkboxClass: 'icheckbox_minimal-blue',
			radioClass: 'iradio_square-blue'
	   	});
		
		$("#showIcon").click(function(){
			window.icons.dialog();
		});
		
		$("#showEasyuiIcon").click(function(){
			window.icons.dialogEasyui();
		});
		
		$cc = $("#cc").combobox({
			url: "${ctx}/manager/system/menu/getAllList",
			valueField: "id", textField: "text", panelHeight: "auto",
			onSelect: function(record) {
				if("tree" === record.mode) {
					$("#treeBox").show();
					$resource.combotree("reload", "${ctx}/manager/system/resource/treegridByMenu/"+record.id);
				} else {
					$resource.combotree("setValue", "");
					$("#treeBox").hide();
				}
			},
			onLoadSuccess: function() {
				$cc.combobox("setValue", "${menuId}");
				var p = $.array.filter($cc.combobox("getData"), function(item){
					return (item.id == "${menuId}")? true : false ;
				});
				if("tree" === p[0].mode) {
					$("#treeBox").show();
					$resource.combotree("reload", "${ctx}/manager/system/resource/treegridByMenu/${menuId}");
				} else {
					$("#treeBox").hide();
				}
			}
		});
		
		$resource = $("#resource").combotree({
			idFiled:'pid', editable: false, required:false, lines:true, autoShowPanel: false,
			onChange: function(newValue, oldValue) {
				var checkId = $("#checkId").val() ;
				if(newValue == checkId) {
					alert("不可选择自己作为父节点");
				}
			}
		});
		
		$permits = $("#combogrid-permit").combogrid({
			url: "${ctx}/manager/system/permit/datagrid",
			panelWidth: 500, multiple: true,
            idField: 'id', textField: 'name',
            border: false, striped:true, pageSize: 30, pageList: [30,40,50,100,200,500,1000], 
            pagination: true, rownumbers: true,
            frozenColumns: [[
                { field: "ck", checkbox: true },
                { field: "id", title: "ID", width: 80, sortable: true, hidden: true }
            ]],
            columns: [[
                {field:'name',title:'权限名称',width:120},
                {field:'identity',title:'权限标示',width:150},
                {field:'description',title:'权限描述',width:320} 
            ]],
            fitColumns: false
		});
		
		if(undefined != id && "" != id) {
			form_url = "${ctx}/manager/system/resource/update" ;
			$.ajax({
				type: "GET", dataType: "JSON",
				url: "${ctx}/manager/system/resource/get/"+id,
				success: function(result) {
					//权限
					var permitIds = [];
					$.each(result.permits, function(i, p){
						permitIds.push(p.id);
					});
					$permits.combogrid("setValues",permitIds);
					
					$("input[name=status][value="+result.status+"]").iCheck('check') ;
					$("input[name=mode][value="+result.mode+"]").iCheck('check') ;
					$("input[name=checked][value="+result.checked+"]").iCheck('check') ;
					$("#checkId").val(result.id);
					
					$("#form").form("load", result);
				},
				complete: function(XMLHttpRequest, textStatus){},
				error: function(XMLHttpRequest, textStatus, errorThrown){
					$.easyui.loaded();
					if(XMLHttpRequest.status == 403) {
						$.messager.alert(XMLHttpRequest.statusText, XMLHttpRequest.responseText, "warning");
					} else {
						$.messager.alert("错误", "发生未知错误", "error");
					}
				}
			});
		}
		
	});
	
	//提交表单数据
	var submitNow = function($d, $tg, flag) {
		var data = $("#form").form("getData");
		var permitIds = $permits.combogrid("getValues");
		if(permitIds.length > 0){
			data["permitIds"] = permitIds.join(",");
		}
		$.post(form_url, data, function(result) {
			if (result.status) {
				$tg.treegrid('reload');alertify.success(result.message);$.easyui.loaded();
				if(flag==undefined||flag==false){$d.dialog("close") ;}
			} else {
				$.easyui.loaded();$.messager.alert("错误", result.message, "error");
			}
		}, 'json').error(function() { $.easyui.loaded(); });
	};
	
	//验证表单
	var submitForm = function($d, $tg, flag) { 
		if($('#form').form('validate')) {
			$.easyui.loading({ msg: "数据提交中，请稍等..." });
			submitNow($d, $tg, flag) ;
		}
	};
</script>

